import {identityGenerator} from './functional.js'
import {makeContinuable} from './iterator.js'

type Serializer<T> = (element: T) => Iterable<string>

export function serializeList<T>(
    items: Iterable<T>,
    serializer: Serializer<T>,
): Iterable<string>
export function serializeList(
    items: Iterable<string>,
    serializer?: Serializer<string>,
): Iterable<string>
export function* serializeList(
    items: Iterable<string>,
    serializer: Serializer<string> = identityGenerator,
): Iterable<string> {
    const iterator = makeContinuable(items)
    const firstResult = iterator.next()

    if (!firstResult.done)
        yield* serializer(firstResult.value)

    for (const value of iterator) {
        yield ', '
        yield* serializer(value)
    }
}
